/* The jewel is a simple diamond polygon.    */
/* It decays and erases itself when age <= 0 */
/* It's offspring appears in a new location. */!!

inherit(Object, #Jewel, #(age      /* remaining lifetime */
eraser   /* erasing device */
pen      /* drawing device */
rock     /* shape of jewel */
rustRate /* jewel's death rate */
x        /* xPos of jewel */
y        /* xPos of jewel */), 2, nil)!!

now(JewelClass)!!

/* Create and initialize a new jewel. */
Def  new(self | jewel, rockSize)
{ jewel := new(self:Behavior);
  jewel.eraser := Call CreatePen(0, 1, 0xFFFFFF);  /* white */
  ^jewel;
}!!

now(Jewel)!!

/* Set up random age, rustRate */
/* and random color to draw.   */
/*******************************/
Def  init(self)
{ age := random(150);
  rustRate := random(4);
  pen :=  Call CreatePen(0, 1, #(0x2000FF, /* red */
                                 0x20FF00, /* green */
                                 0xFF0000, /* blue */
                                 0xFF009F  /* purple */
          )[rustRate]);
  rock := new(WinPolygon,            /* diamond */
   tuple(point(x-3,y), point(x,y-3),
         point(x+3,y), point(x,y+3),
         point(x-3,y), point(x-2,y),
         point(x,y+2), point(x+2,y),
         point(x,y-2),  point(x-2,y)));
}!!

/* Return the radius in pixels. */
/********************************/
Def  size(self)
{ ^3
}!!

/* Return the remaining lifetime of the jewel */
/**********************************************/
Def  age(self)
{ ^age
}!!

/* Decrement the jewel's lifetime. */
/***********************************/
Def  rust(self)
{ age := age - rustRate;
}!!

/* Return the yPos of jewel. */
/*****************************/
Def  y(self)
{ ^y
}!!

/* Return the xPos of jewel. */
/*****************************/
Def  x(self)
{ ^x
}!!

/* Erase the jewel by drawing the bkgd color. */
/**********************************************/
Def  erase(self, hDC)
{ Call SelectObject(hDC, eraser);
  draw(rock, hDC);
}!!

/* Display the jewel with the selected color. */
/* If this is the first draw, set a position  */
/*   in the field rectangle.                  */
/**********************************************/
Def  draw(self, hDC, field)
{ if x = nil cor y = nil
  then x := random(right(field)-size(self));
       y := random(bottom(field)-size(self));
       init(self);
  endif;
  Call SelectObject(hDC, pen);
  draw(rock, hDC);
}!!

